package my.menu.menuImp;

import my.dao.daoImpl.UserDao;
import my.menu.Menu;
import my.vo.voImpl.User;

import java.util.List;

/**
 * @author 星环
 * @date 2022/10/31 13:48:08
 * @description 同 {@link AdminMainMenu}
 */
public class LoginMenu extends Menu {
    private final UserDao userDao = UserDao.getInstance();
    //用于后续菜单获得登录用户信息
    private User user;

    public LoginMenu(){
        //dao、super注册功能
        super(  List.of("注册","登录"),
                List.of("regis","login"));
    }

    /**
     * 用于调用者获取用户信息
     * @return 登录的用户信息
     */
    public User getUser(){
        return user;
    }

    protected boolean login(){
        System.out.println("请输入用户名和密码");
        String username = sc.next();
        String password = sc.next();

        user = userDao.login(username,password);


        if(user == null){
            System.out.println("用户名或密码错误");
        }else {
            System.out.println("登录成功！");
        }
        //用户登录成功，则退出循环，所以继续的条件是user == null
        return user == null;
    }

    protected boolean regis(){
        System.out.println("请输入用户名和密码");
        String username = sc.next();
        String password = sc.next();

        User newUser = userDao.register(username, password);


        if(newUser == null){
            System.out.println("注册失败，用户名重复");
            return true;
        }
        System.out.println("注册成功！");
        System.out.println(newUser);
        return true;
    }

}
